﻿import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4

Item{
    id:root
    implicitWidth: 200
    implicitHeight: 40
    property alias model: lv.model
    property var indexs: []
    property var m_text: ""
    property int flag: 0
    property bool csvis: false

    onVisibleChanged: {
        csvis=false
    }


    function removeValue(arr,val) {      //删除数组中指定的内容arr数组，val内容
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == val) {
                arr.splice(i, 1)
                return arr;
            }
        }
    }
    // get方法，用于获取m_text的值
    function getText() {
        return m_text;
    }

    // set方法，用于设置m_text的值
    function setText(newText) {
        m_text=newText
        lv.model=model
    }
    function resetSelection() {
        for (var i = 0; i < lv.count; i++) {
            var item = lv.itemAt(i);
            if (item && item.rect) {
                item.rect.isSelect = false;
                item.rect.color = "white";
                item.fonttext.color = "black";
            }
        }
    }


    Button{
        id: btn
        anchors.fill: parent
        Text{
            anchors.fill: parent
            anchors.margins: 5
            anchors.rightMargin: 30
            text: m_text
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            font.pixelSize: config.pointsize + 2*dp
            clip: true
            elide: Text.ElideMiddle
        }

        onClicked: {
            csvis = !csvis;
        }
        Image{
            width: 25*dp
            height: 20*dp
            x:parent.width-30*dp
            y:10*dp
            source:"qrc:/picture/downarrow.png"
        }
    }

    Component{
        id: m_del
        Rectangle{
            id:rect
            color:"white"
            implicitWidth: 200
            implicitHeight: 40
            property bool isSelect: {
                var items = m_text.split(",");  // 先将m_text按逗号分割成数组
                for (var j = 0; j < items.length; j++) {
                    if (items[j].trim() === fonttext.text) {  // 对比分割后的每一项（去除空格后）与当前项文本是否相等
                        fonttext.color="#1677FF"
                        return true;
                    }
                }
                fonttext.color="black"
                return false;
            }

            border.color: "#EDEDED"
            Text{
                id:fonttext
                anchors.fill: parent
                horizontalAlignment:Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                font.pixelSize: config.fontsize + 3*dp
                text: label
                color: "black"
            }

            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                onEntered: {
                    if(!isSelect){
                        rect.color = "#F2F9FF";
                    }
                }
                onExited: {
                    if(!isSelect){
                        rect.color = "white"
                    }
                }
                onClicked: {
                    rect.isSelect = !rect.isSelect;
                    if(!rect.isSelect){         //取消某项
                        rect.color = "white"
                        fonttext.color="black"
                        //删除文本中指定的字符串
                        var arr = m_text.split(",");
                        arr = removeValue(arr,label)
                        m_text = arr.join(",");
                        //删除数组中记录指定的内容
                        indexs = removeValue(indexs,index);
                    }
                    else{                       //选择某项
                        rect.color = "white"
                        fonttext.color="#1677FF"
                        if(m_text.length == 0)
                        {
                            m_text += label;
                        }
                        else{
                            m_text += "," + label;
                        }
                        indexs.push(index)
                    }

                }
            }
        }
    }

    ScrollView{
        id:sv
        anchors.top:btn.bottom
        anchors.left: parent.left
        width: parent.width
        height: 300
        visible: csvis

        ListView{
            id: lv
            delegate:m_del
            anchors.fill: parent
        }
    }

}
